package com.haixiaoke.saas.house.service.impl;

import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.exception.ServiceException;
import com.haixiaoke.saas.common.utils.DateUtils;
import com.haixiaoke.saas.house.domain.House;
import com.haixiaoke.saas.house.domain.HouseLead;
import com.haixiaoke.saas.house.mapper.HouseLeadMapper;
import com.haixiaoke.saas.house.service.IHouseLeadService;
import com.haixiaoke.saas.house.service.IHouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 房屋留咨信息Service业务层处理
 *
 * @author qixi
 * @date 2025-07-08
 */
@Service
public class HouseLeadServiceImpl implements IHouseLeadService {
    @Autowired
    private HouseLeadMapper houseLeadMapper;
    @Autowired
    private IHouseService houseService;

    /**
     * 查询房屋留咨信息
     *
     * @param leadId 房屋留咨信息主键
     * @return 房屋留咨信息
     */
    @Override
    public HouseLead selectHouseLeadByLeadId(Long leadId) {
        return houseLeadMapper.selectHouseLeadByLeadId(leadId);
    }

    /**
     * 查询房屋留咨信息列表
     *
     * @param houseLead 房屋留咨信息
     * @return 房屋留咨信息
     */
    @Override
    public List<HouseLead> selectHouseLeadList(HouseLead houseLead) {
        return houseLeadMapper.selectHouseLeadList(houseLead);
    }

    /**
     * 新增房屋留咨信息
     *
     * @param houseLead 房屋留咨信息
     * @return 结果
     */
    @Override
    public AjaxResult insertHouseLead(HouseLead houseLead) {
        if (StrUtil.isBlank(houseLead.getPhone())) {
            return AjaxResult.error("手机号不能为空");
        }
        House house = houseService.selectHouseByHouseId(houseLead.getHouseId());
        if (ObjectUtil.isEmpty(house)) {
            throw new ServiceException("房源不存在");
        }
        houseLead.setCreateTime(DateUtils.getNowDate());
        int result = houseLeadMapper.insertHouseLead(houseLead);
        return result > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 修改房屋留咨信息
     *
     * @param houseLead 房屋留咨信息
     * @return 结果
     */
    @Override
    public int updateHouseLead(HouseLead houseLead) {
        houseLead.setUpdateTime(DateUtils.getNowDate());
        return houseLeadMapper.updateHouseLead(houseLead);
    }

    /**
     * 批量删除房屋留咨信息
     *
     * @param leadIds 需要删除的房屋留咨信息主键
     * @return 结果
     */
    @Override
    public int deleteHouseLeadByLeadIds(Long[] leadIds) {
        return houseLeadMapper.deleteHouseLeadByLeadIds(leadIds);
    }

    /**
     * 删除房屋留咨信息信息
     *
     * @param leadId 房屋留咨信息主键
     * @return 结果
     */
    @Override
    public int deleteHouseLeadByLeadId(Long leadId) {
        return houseLeadMapper.deleteHouseLeadByLeadId(leadId);
    }

    @Override
    public int countTotal() {
        return houseLeadMapper.countTotal();
    }
}
